package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishflavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.Dishservice;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
@Slf4j
public class Dishserviceimpl implements Dishservice {
//    进行多表操作，事物处理
@Autowired
      private DishMapper dishMapper;
@Autowired
private DishflavorMapper dishflavorMapper;
@Autowired
private SetmealDishMapper setmealDishMapper;
    @Transactional
    public void savewithflavor(DishDTO dishDTO) {
        //向菜品表插入一条数据
    //因为执行菜品表，所以传递菜品的实体类
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);//属性拷贝
        dishMapper.insert(dish);
        Long dishId = dish.getId();//执行完insert语句口程序自动给dish赋值id，获取即可
        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            dishflavorMapper.insertBatch(flavors);//sql支持批量插入，直接将集合传入
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
//    判断能否删除 是否为起售中的商品
        for (Long id : ids) {
       Dish dish=  dishMapper.getById(id);
       if(dish.getStatus()== StatusConstant.ENABLE){
              //不能删除，直接抛出异常
           throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
       }
        }
//    判断能否删除，是否关联套餐
        List<Long> setmealIds = setmealDishMapper.getSetmealsIdsByDishIds(ids);
        if(setmealIds!=null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
/*//    删除菜品数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
//    删除口味数据
            dishflavorMapper.deleteByDishId(id);
        }*/


//        批量删除菜品数据
        dishMapper.deleteByIds(ids);
//        批量删除口味数据
dishflavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO getByIdwithFlavor(Long id) {
//        根据菜品id查询菜品数据、
        Dish dish = dishMapper.getById(id);
//        根据菜品id查询口味数据
        List<DishFlavor> dishFlavors= dishflavorMapper.getByDishId(id);
//        封装菜品和口味数据
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);//不想一个一个赋值，拷贝
        dishVO.setFlavors(dishFlavors);
       return dishVO;
    }

    @Override
    public void updatewithflavor(DishDTO dishDTO) {
        //修改菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        //由于口味的情况繁多，有添加，修改，空 可以直接删除原有数据再进行添加
        dishflavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
            dishflavorMapper.insertBatch(flavors);//sql支持批量插入，直接将集合传入
        }

    }


}
